/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ventanas;

/**
 * Sample Skeleton for "VentanaSecundariaFXML.fxml" Controller Class You can
 * copy and paste this code into your favorite IDE
 *
 */
import bbdd.classes.Coche;
import bbdd.classes.RutaDet2;
import bbdd.classes.TrutaMae;
import bbdd.mysql.ConexionMysql;
import bbdd.sqlite.ConexionSqlLite;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;
import name.antonsmirnov.javafx.dialog.Dialog;

public class VentanaSecundariaFXMLController
        implements Initializable {

    ConexionMysql connMysql;
    ConexionSqlLite connSqlLite;
    @FXML //  fx:id="botonCancelar"
    private Button botonCancelar; // Value injected by FXMLLoader
    @FXML //  fx:id="botonCopiarDatos"
    private Button botonCopiarDatos; // Value injected by FXMLLoader
    @FXML //  fx:id="botonNuevoCoche"
    private Button botonNuevoCoche; // Value injected by FXMLLoader
    @FXML //  fx:id="boxCoche"
    private ComboBox<Coche> boxCoche; // Value injected by FXMLLoader
    @FXML //  fx:id="boxNombreCoche"
    private TextField boxNombreCoche; // Value injected by FXMLLoader
    @FXML //  fx:id="boxTrayecto"
    private ComboBox<TrutaMae> boxTrayecto; // Value injected by FXMLLoader
    @FXML //  fx:id="colGprmc"
    private TableColumn<RutaDet2, String> colGprmc; // Value injected by FXMLLoader
    @FXML //  fx:id="colIdruta"
    private TableColumn<RutaDet2, String> colIdruta; // Value injected by FXMLLoader
    @FXML //  fx:id="colcrc"
    private TableColumn<RutaDet2, String> colcrc; // Value injected by FXMLLoader
    @FXML //  fx:id="colcurrentLAt"
    private TableColumn<RutaDet2, String> colcurrentLAt; // Value injected by FXMLLoader
    @FXML //  fx:id="colcurrentLon"
    private TableColumn<RutaDet2, String> colcurrentLon; // Value injected by FXMLLoader
    @FXML //  fx:id="coldataStamp"
    private TableColumn<RutaDet2, String> coldataStamp; // Value injected by FXMLLoader
    @FXML //  fx:id="colew"
    private TableColumn<RutaDet2, String> colew; // Value injected by FXMLLoader
    @FXML //  fx:id="colns"
    private TableColumn<RutaDet2, String> colns; // Value injected by FXMLLoader
    @FXML //  fx:id="colspeed"
    private TableColumn<RutaDet2, String> colspeed; // Value injected by FXMLLoader
    @FXML //  fx:id="coltimeStamp"
    private TableColumn<RutaDet2, String> coltimeStamp; // Value injected by FXMLLoader
    @FXML //  fx:id="coltrueCourse"
    private TableColumn<RutaDet2, String> coltrueCourse; // Value injected by FXMLLoader
    @FXML //  fx:id="colvalidSt"
    private TableColumn<RutaDet2, String> colvalidSt; // Value injected by FXMLLoader
    @FXML //  fx:id="rootPane"
    private AnchorPane rootPane; // Value injected by FXMLLoader
    @FXML //  fx:id="tableDatos"
    private TableView<RutaDet2> tableDatos; // Value injected by FXMLLoader

    // Handler for Button[fx:id="botonCancelar"] onAction
    public void handleBotonCancelar(ActionEvent event) {
        this.rootPane.getScene().getWindow().hide();
    }

    // Handler for Button[fx:id="botonCopiarDatos"] onAction
    public void handleBotonCopiarDatos(ActionEvent event) {
        Coche coche = this.boxCoche.getSelectionModel().getSelectedItem();
        TrutaMae ruta = this.boxTrayecto.getSelectionModel().getSelectedItem();
        if (coche == null) {
            Dialog.showWarning("Información", "Debe seleccionar un coche");
        } else if (ruta == null) {
            Dialog.showWarning("Información", "Debe Seleccionar una ruta");
        } else {
            List<RutaDet2> listaTrayecto = connSqlLite.getTrayecto(ruta.getIdruta());
            int idRuta = connMysql.insertarRuta(coche.getIdCoche(), ruta);
            if (idRuta > 0 && connMysql.insertarTrayecto(idRuta, listaTrayecto)) {
                Dialog.showInfo("Información", "El trayecto se ha insertado correctamente");
            } else {
                Dialog.showError("Información", "Ha ocurrido un error al insertar la ruta");
            }
        }
    }

    // Handler for Button[fx:id="botonNuevoCoche"] onAction
    public void handleBotonNuevoCoche(ActionEvent event) {
        if (this.boxNombreCoche.getText().isEmpty()) {
            Dialog.showError("Información", "Debe introcudir una descripcion del coche");
        } else {
            if (connMysql.insertarCoche(boxNombreCoche.getText())) {
                this.boxNombreCoche.setText("");
                Dialog.showInfo("Información", "Coche introducido correctamente");
                recargarBoxCoches();
            }
        }
    }

    // Handler for ComboBox[fx:id="boxCoche"] onAction
    public void handleBoxCoche(ActionEvent event) {
        // handle the event here
    }

    // Handler for ComboBox[fx:id="boxTrayecto"] onAction
    public void handleBoxTrayecto(ActionEvent event) {
        TrutaMae ruta = boxTrayecto.getSelectionModel().getSelectedItem();
        if (ruta != null) {
            List<RutaDet2> listaTrayecto = connSqlLite.getTrayecto(ruta.getIdruta());
            recargarTable(listaTrayecto);
        }
    }

    @Override // This method is called by the FXMLLoader when initialization is complete
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        assert botonCancelar != null : "fx:id=\"botonCancelar\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert botonCopiarDatos != null : "fx:id=\"botonCopiarDatos\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert botonNuevoCoche != null : "fx:id=\"botonNuevoCoche\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert boxCoche != null : "fx:id=\"boxCoche\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert boxNombreCoche != null : "fx:id=\"boxNombreCoche\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert boxTrayecto != null : "fx:id=\"boxTrayecto\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert colGprmc != null : "fx:id=\"colGprmc\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert colIdruta != null : "fx:id=\"colIdruta\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert colcrc != null : "fx:id=\"colcrc\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert colcurrentLAt != null : "fx:id=\"colcurrentLAt\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert colcurrentLon != null : "fx:id=\"colcurrentLon\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert coldataStamp != null : "fx:id=\"coldataStamp\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert colew != null : "fx:id=\"colew\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert colns != null : "fx:id=\"colns\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert colspeed != null : "fx:id=\"colspeed\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert coltimeStamp != null : "fx:id=\"coltimeStamp\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert coltrueCourse != null : "fx:id=\"coltrueCourse\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert colvalidSt != null : "fx:id=\"colvalidSt\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert rootPane != null : "fx:id=\"rootPane\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";
        assert tableDatos != null : "fx:id=\"tableDatos\" was not injected: check your FXML file 'VentanaSecundariaFXML.fxml'.";

        // initialize your logic here: all @FXML variables will have been injected

    }

    void setConexiones(ConexionSqlLite connSqlite, ConexionMysql connMysql) {
        this.connMysql = connMysql;
        this.connSqlLite = connSqlite;

        recargarBoxCoches();
        recargarBoxRutas();
    }

    private void recargarBoxCoches() {
        List<Coche> listaCoches = connMysql.getCoches();
        if (!listaCoches.isEmpty()) {
            ObservableList<Coche> obListaCoches = FXCollections.observableList(listaCoches);
            this.boxCoche.setItems(obListaCoches);
        } else {
            Logger.getLogger(VentanaSecundariaFXMLController.class.getName()).log(Level.WARNING, "No hay coches definidos");
        }
    }

    private void recargarBoxRutas() {
        List<TrutaMae> listaRutas = connSqlLite.getRutas();
        if (!listaRutas.isEmpty()) {
            ObservableList<TrutaMae> obListaRutas = FXCollections.observableList(listaRutas);
            this.boxTrayecto.setItems(obListaRutas);
        } else {
            Logger.getLogger(VentanaSecundariaFXMLController.class.getName()).log(Level.WARNING, "No hay rutas para copiar");
        }
    }

    private void recargarTable(List<RutaDet2> listaTrayecto) {

        this.colIdruta.setCellValueFactory(new PropertyValueFactory<RutaDet2, String>("idRuta"));
        this.colGprmc.setCellValueFactory(new PropertyValueFactory<RutaDet2, String>("gprmc"));
        this.coltimeStamp.setCellValueFactory(new Callback<CellDataFeatures<RutaDet2, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<RutaDet2, String> p) {
                return new SimpleStringProperty(p.getValue().getTimeStampString());
            }
        });
        this.colvalidSt.setCellValueFactory(new PropertyValueFactory<RutaDet2, String>("validSt"));
        this.colcurrentLAt.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<RutaDet2, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<RutaDet2, String> p) {
                return new SimpleStringProperty(p.getValue().getCurrentLat().toString());
            }
        });
        this.colns.setCellValueFactory(new PropertyValueFactory<RutaDet2, String>("ns"));
        this.colcurrentLon.setCellValueFactory(new Callback<CellDataFeatures<RutaDet2, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<RutaDet2, String> p) {
                return new SimpleStringProperty(p.getValue().getCurrentLon().toString());
            }
        });
        this.colew.setCellValueFactory(new PropertyValueFactory<RutaDet2, String>("ew"));
        this.colspeed.setCellValueFactory(new Callback<CellDataFeatures<RutaDet2, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<RutaDet2, String> p) {
                return new SimpleStringProperty(p.getValue().getSpeed().toString());
            }
        });
        this.coltrueCourse.setCellValueFactory(new Callback<CellDataFeatures<RutaDet2, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<RutaDet2, String> p) {
                return new SimpleStringProperty(p.getValue().getTrueCourse().toString());
            }
        });
        this.coldataStamp.setCellValueFactory(new Callback<CellDataFeatures<RutaDet2, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<RutaDet2, String> p) {
                return new SimpleStringProperty(p.getValue().getDataStamp().toString());
            }
        });
        this.colcrc.setCellValueFactory(new PropertyValueFactory<RutaDet2, String>("crc"));
        ObservableList<RutaDet2> obLitaTrayecto = FXCollections.observableList(listaTrayecto);
        this.tableDatos.setItems(obLitaTrayecto);
    }
}
